%%%-------------------------------------------------------------------
%%% @author admin
%%% @copyright (C) 2020, <COMPANY>
%%% @doc
%%%
%%% @end
%%% Created : 8. 4月 2020 11:20
%%%-------------------------------------------------------------------
-module(emqx_urap_connection).
-author("wwhai").

-behaviour(gen_server).
-define(BYTE, 8).
%% API
-export([start_link/2, init/2]).

%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2,
    code_change/3]).

-define(SERVER, ?MODULE).
-define(UDP_OPTS, [binary, {reuseaddr, true}]).

-record(emqx_urap_state, {}).

%%%===================================================================
%%% API
% %%%===================================================================
% start(Port) ->
%   Opts = [{udp_options, [binary, {reuseaddr, true}]}],
%   MFA = {?MODULE, start_link, []},
%   esockd:open_udp('urap:udp', Port, Opts, MFA).


start_link(Transport, Peer) ->
    {ok, proc_lib:spawn_link(?MODULE, init, [Transport, Peer])}.

init(_T, _P) ->
    gen_server:enter_loop(?MODULE, [], #emqx_urap_state{}).

init(_) -> ok.

%%
%% 消息接收处理
%%

handle_call(_Request, _From, State) ->
    {reply, ok, State}.

%% UDP data redirect to "$udp" topic
%% default QOS=1
handle_info(Binary, State) ->
    case Binary of
        {datagram, _, RawData} ->
%%            io:format("---------------------------------------------------------------~n"),
%%            binpp:pprint(RawData),
%%            io:format("---------------------------------------------------------------~n"),

            try
                <<ClientIdLength:8, ClientId:ClientIdLength/binary, PayloadLength:8, Payload:PayloadLength/binary>> = RawData,
                Message = emqx_message:make(ClientId, 1, <<"$udp">>, Payload),
                emqx:publish(Message)
            catch
                Reason:E ->
                    io:format("Datagram error :~p Reason : ~p ~n", [E, Reason])
            end
    end,
    {noreply, State}.

terminate(_Reason, _State) ->
    ok.

code_change(_OldVsn, State, _Extra) ->
    {ok, State}.


handle_cast(_Msg, State) ->
    {noreply, State}.
